package org.balabala.user.service.impl;


import com.alibaba.fastjson.TypeReference;
import org.balabala.user.dao.BrowseInfoDao;
import org.balabala.user.dao.UserDao;
import org.balabala.user.entity.BrowseInfoEntity;
import org.balabala.user.entity.UserEntity;
import org.balabala.user.entity.VideoEntity;
import org.balabala.user.feign.VideoFeign;
import org.balabala.user.service.BrowseInfoService;
import org.balabala.user.utils.PageUtils;
import org.balabala.user.utils.Query;
import org.balabala.user.utils.Resp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.Key;
import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;


@Service("browseInfoService")
public class BrowseInfoServiceImpl extends ServiceImpl<BrowseInfoDao, BrowseInfoEntity> implements BrowseInfoService {

    @Autowired
    private VideoFeign videoFeign;

    @Resource
    private UserDao userDao;
    /**
     *
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        //this.page(Ipage,查询条件）；
        //Query是自己写的一个接口，调用getPage方法以后返回Ipage对象
        IPage<BrowseInfoEntity> page = this.page(
                new Query<BrowseInfoEntity>().getPage(params),
                new QueryWrapper<BrowseInfoEntity>()
        );

        return new PageUtils(page);
    }

    //通过指定uid查询浏览信息，默认第一页，一页十条信息
    @Override
    public List<VideoEntity> queryPageByUid(Map<String, Object> params) {
        IPage<BrowseInfoEntity> page = this.page(
                new Query<BrowseInfoEntity>().getPage(params),
                new QueryWrapper<BrowseInfoEntity>().eq("uid",params.get("uid"))
        );
        List<BrowseInfoEntity> records = page.getRecords();
        List<Long> collect = records.stream().map(item -> item.getVideoId()).collect(Collectors.toList());
        Long[] videoIds = collect.toArray(new Long[]{});
        //调用用户服务，进行视频信息的查询
        Resp resp = videoFeign.listByUid(videoIds);
        List<VideoEntity> data = resp.getData(new TypeReference<List<VideoEntity>>() {
        });

        return data;
    }

    @Override
    public List<UserEntity> queryPageByVideoId(Map<String, Object> params) {
        IPage<BrowseInfoEntity> page = this.page(
                new Query<BrowseInfoEntity>().getPage(params),
                new QueryWrapper<BrowseInfoEntity>().eq("video_id",params.get("videoid"))
        );
        List<BrowseInfoEntity> records = page.getRecords();
        if(records == null){
            return null;
        }
        List<Long> collect = records.stream().map(item -> item.getUid()).collect(Collectors.toList());
        //通过用户id查询用户信息
        List<UserEntity> userEntities = userDao.selectBatchIds(collect);
        return userEntities;
    }

    @Override
    public Resp removeByBrowseId(Integer browseId) {
        int browse_id = this.baseMapper.delete(new QueryWrapper<BrowseInfoEntity>().eq("browse_id", browseId));
        if(browse_id == 0){
            return  Resp.error(500,"删除失败,请确认是否存在该项浏览记录");
        }else{
            return  Resp.ok();
        }
    }

    @Override
    public Resp saveBrowse(BrowseInfoEntity browseInfo) {
        browseInfo.setBrowseId(System.currentTimeMillis());
        browseInfo.setBrowseTime(new Date());
        int insert = this.baseMapper.insert(browseInfo);
        if(insert == 0){
            return Resp.error(500,"添加失败");
        }else {
            return Resp.ok();
        }
    }

    @Override
    public PageUtils batchList(Map<String, Object> map) {
        IPage<BrowseInfoEntity> page = this.page(
                new Query<BrowseInfoEntity>().getPage(map),
                new QueryWrapper<BrowseInfoEntity>().like("browse_time",map.get("key")).or().like("user_id",map.get("key")).or().like("video_id",map.get("key")).orderByDesc("browse_time")
        );
        return new PageUtils(page);
    }

    /*//查询视频的浏览量
    @Override
    public Long browseCount(Long videoid) {
        this.baseMapper.selectCount(new QueryWrapper<>())
        return null;
    }*/




    /*//通过指定videoid查询评论信息，默认第一页，一页十条信息
    @Override
    public PageUtils queryPageByVideoId(Map<String, Object> params) {
        IPage<BrowseInfoEntity> page = this.page(
                new Query<BrowseInfoEntity>().getPage(params),
                new QueryWrapper<BrowseInfoEntity>().eq("video_id",params.get("videoid"))
        );
        List<BrowseInfoEntity> records = page.getRecords();
        List<Long> collect = records.stream().map(item -> item.getUid()).collect(Collectors.toList());
        //查询数据库信息
        return new PageUtils(page);
    }*/


    @Override
    public boolean saveBatch(Collection<BrowseInfoEntity> entityList, int batchSize) {
        return super.saveBatch(entityList, batchSize);
    }
}